﻿@page
@model IndexModel
@{
    ViewData["Title"] = "LoggerMessage Sample";
}

<h1>@ViewData["Title"]</h1>

<div class="row">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">Instructions</h3>
        </div>
        <div class="panel-body">
            <ol>
                <li>
                    On a GET request for this page, the logger logs a message:
                    <pre><code>info: LoggerMessageSample.Pages.IndexModel[1]
=> RequestId:0HL90M6E7PHK4:00000001 RequestPath:/ => /Index
GET request for Index page</code></pre>
                </li>
                <li>
                    Add a message.
                </li>
                <li>
                    The console indicates the logged message for the add operation:
                    <pre><code>info: LoggerMessageSample.Pages.IndexModel[2]
=> RequestId:0HL90M6E7PHK5:0000000A RequestPath:/ => /Index
Quote added (Quote = 'You can avoid reality, but you cannot avoid the consequences of avoiding reality. - Ayn Rand')</code></pre>
                </li>
                <li>
                    Delete the quote using its <b>Delete</b> button.
                </li>
                <li>
                    The console shows a log entry for the delete opertion:
                    <pre><code>info: LoggerMessageSample.Pages.IndexModel[4]
=> RequestId:0HL90M6E7PHK5:00000016 RequestPath:/ => /Index
Quote deleted (Quote = 'You can avoid reality, but you cannot avoid the consequences of avoiding reality. - Ayn Rand' Id = 1)</code></pre>
                <li>
                    Add three quotes.
                </li>
                <li>
                    Select the <b>Clear All</b> button.
                </li>
                <li>
                    The console includes the scope message:
                    <pre><code>info: LoggerMessageSample.Pages.IndexModel[4]
=> RequestId:0HL90MKUCAN0A:0000000A RequestPath:/ => /Index => All quotes deleted (Count = 3)
Quote deleted (Quote = 'Quote 1' Id = 1)
info: LoggerMessageSample.Pages.IndexModel[4]
=> RequestId:0HL90MKUCAN0A:0000000A RequestPath:/ => /Index => All quotes deleted (Count = 3)
Quote deleted (Quote = 'Quote 2' Id = 2)
info: LoggerMessageSample.Pages.IndexModel[4]
=> RequestId:0HL90MKUCAN0A:0000000A RequestPath:/ => /Index => All quotes deleted (Count = 3)
Quote deleted (Quote = 'Quote 3' Id = 3)</code></pre>
                </li>
            </ol>
        </div>
    </div>

    <form method="post">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Add a quote</h3>
            </div>
            <div class="panel-body">
                <div class="form-group">
                    <label asp-for="Quote.Text"></label>
                    <input asp-for="Quote.Text" class="form-control" style="width:100%;margin-bottom:5px">
                </div>
                <div class="form-group">
                    <button type="submit" asp-page-handler="AddQuote" class="btn btn-default">Add Quote</button>
                </div>
            </div>
        </div>
    </form>

    <form method="post">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <button type="submit" asp-page-handler="DeleteAllQuotes" class="pull-right btn btn-danger">Clear All</button>
                <h3 class="panel-title" style="line-height:2.1">Quotes</h3>
            </div>
            <div class="panel-body">
                <ol>
                    @foreach (var quote in Model.Quotes)
                    {
                        <li>
                            <button type="submit" class="btn btn-danger" style="margin-bottom:2px" asp-page-handler="DeleteQuote" asp-route-id="@quote.Id">Delete</button>
                            <p style="line-height:2.5">@quote.Text</p>
                        </li>
                    }
                </ol>
            </div>
        </div>
    </form>
</div>

<div class="row">
    <form method="post">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title" style="line-height:2.1">Log an exception</h3>
            </div>
            <div class="panel-body">
                <ol>
                    <li>
                        A quote with an <code>Id = 999</code> doesn't exist in the database. Ordinarily, you would code the <code>OnPostDeleteQuoteAsync</code> method to detect for a <code>null</code> message returned by <code>_db.Quotes.FindAsync(id)</code>. The sample app doesn't check for <code>null</code>. The sample app surrounds the database <code>Remove</code> method in a <code>try</code>-<code>catch</code> block. When <code>Remove</code> throws an <code>ArgumentNullException</code>, the <code>QuoteDeleteFailed</code> method is triggered on the logger.
                    </li>
                    <li>
                        Select the <b>Delete (Id = 999)</b> button and examine the console log for the message, which includes the <code>ArgumentNullException</code>.
                        <pre><code>LoggerMessageSample.Pages.IndexModel: Error: Quote delete failed (Id = 999)

System.NullReferenceException: Object reference not set to an instance of an object.
   at lambda_method(Closure , ValueBuffer )
   at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
   at Microsoft.EntityFrameworkCore.InMemory.Query.Internal.InMemoryShapedQueryCompilingExpressionVisitor.AsyncQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
   at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
   at LoggerMessageSample.Pages.IndexModel.OnPostDeleteQuoteAsync(Int32 id) in c:\Users\guard\Documents\GitHub\Docs\aspnetcore\fundamentals\logging\loggermessage\samples\3.x\LoggerMessageSample\Pages\Index.cshtml.cs:line 77</code></pre>
                    </li>
                </ol>
                <button type="submit" class="btn btn-danger" style="margin-bottom:2px" asp-page-handler="DeleteQuote" asp-route-id="999">Delete (Id = 999)</button>
            </div>
        </div>
    </form>
</div>
